package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class srv_alumnosNotas extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Connection cn = null;
        HttpSession session = request.getSession(true);
        try {
            String curso = request.getParameter("curso");
            response.setContentType("text/html");
            ArrayList<Map> lstAlumnosNot = new ArrayList<Map>();
            // conectamos la BD 
            GestionBD og = new GestionBD();
            cn = og.conectar();
            String query = "select id_alumno,not_practica1,not_practica2,not_practica3,not_practica4,not_prompractica,not_examparcial,not_examfinal,not_promfinal"
                    + " from nota where id_curso="+curso;
            Statement stm = cn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            while (rs.next()) {
                Map rec = new HashMap();
                // obtenemos un curso
                rec.put("id_alumno", rs.getInt("id_alumno"));
                rec.put("not_practica1", rs.getInt("not_practica1"));
                rec.put("not_practica2", rs.getInt("not_practica2"));
                rec.put("not_practica3", rs.getInt("not_practica3"));
                rec.put("not_practica4", rs.getInt("not_practica4"));
                rec.put("not_prompractica", rs.getInt("not_prompractica"));
                rec.put("not_examparcial", rs.getInt("not_examparcial"));
                rec.put("not_examfinal", rs.getInt("not_examfinal"));
                rec.put("not_promfinal", rs.getInt("not_promfinal"));
                // lo almacenamos en el array
                lstAlumnosNot.add(rec);
            }
            session.setAttribute("lista_aluNot", lstAlumnosNot);
            stm.close();
            //cn.close();
        } catch (Exception e) {
            System.out.println("problemas al listar alumnos matriculados");
        }
        // generamos la vista y lo devolvemos al cliente
        RequestDispatcher rd = request.getRequestDispatcher("listaAlumnoNotas.jsp");
        rd.forward(request, response);
    }
}
